package com.hxkj.service.article.impl;


import com.alibaba.fastjson.JSONObject;
import com.hxkj.client.service.CompanyInfoServiceClient;
import com.hxkj.domain.enums.ArticleExceptionEnum;
import com.hxkj.domain.enums.CompanyInfoExceptionEnum;
import com.hxkj.domain.enums.CompanyInfoSuccessEnum;
import com.hxkj.domain.po.Article;
import com.hxkj.domain.po.AuthUser;
import com.hxkj.domain.po.Companyinfo;
import com.hxkj.domain.po.FreeMarker;
import com.hxkj.service.article.CompanyInfoService;
import com.hxkj.token.utils.TokenUtils;
import com.hxkj.utils.mapper.ArticleMapper;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 合作公司实现类
 * <p>
 * Created by yaosy on 2018年6月8日13:58:01
 */
@Service
@Slf4j
public class CompanyInfoServiceImpl implements CompanyInfoService {

    @Autowired
    private CompanyInfoServiceClient companyInfoServiceClient;

    @Autowired
    private TokenUtils tokenUtils;

    @Override
    public ResultPageStruct<Companyinfo> getList(Companyinfo companyinfo, PageTo pageParam)throws Exception {
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        ResultPageStruct<Companyinfo> result = new ResultPageStruct<>();
        ParameterDataStruct<Companyinfo> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(companyinfo);
        parameterDataStruct.setPageTo(pageParam);
        try{
            result = companyInfoServiceClient.getList(parameterDataStruct);
            List<Companyinfo> companyinfoList = result.getData();

        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        result.setMessage(null);
        result.setCode(null);
        return result;
    }

    @Override
    public Companyinfo get(Companyinfo companyinfo)throws Exception {
        ResultDataStruct<Companyinfo> resultDataStruct = new ResultDataStruct<>();
        try{
            Long companyId = companyinfo.getCompanyId();
            if(companyId==null){
                throw new CustomException(CompanyInfoExceptionEnum.COMPANY_INFO_SAVE_FAILED);
            }
            resultDataStruct = companyInfoServiceClient.get(companyinfo);
            log.info("数据为{}",resultDataStruct.getData());
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        return resultDataStruct.getData();
    }

    @Override
    public ResultMessageStruct save(Companyinfo companyinfo) throws Exception{
        ResultDataStruct<Companyinfo> resultDataStruct = new ResultDataStruct<>();
        try{
            throwException(companyinfo);
            AuthUser authUser = (AuthUser) tokenUtils.getTokenUserInfo();
            companyinfo.setCreator(authUser.getUserId());
            companyinfo.setModifier(authUser.getUserId());
            companyinfo.setCreateTime(System.currentTimeMillis());
            companyinfo.setUpdateTime(System.currentTimeMillis());
            resultDataStruct = companyInfoServiceClient.save(companyinfo);
            if(resultDataStruct.getCode() != null){
                throw new CustomException(CompanyInfoExceptionEnum.getEnumByCode(resultDataStruct.getCode()));
            }
        }catch(Exception e){
            throw e;
        }

        // 当公司新增成功后，调用模板解析接口生成静态模板推送至 静态资源
/*        try {
            //调用公用模板解析接口 解析文章详情生成文章详情静态模块
            if (companyinfo.getStatus().equals(0)) {
                Companyinfo com = resultDataStruct.getData();
                FreeMarker model = new   FreeMarker();
                model = ArticleMapper.articMapper(art);
                //model.setData(JSONObject.toJSONString(article));
                JSONObject json =freeMakerService.analyze(model);
                art.setManuscriptPath(json.getString("filePath"));
                articleServiceClient.updateArticle(art);
            }
            if (resultDataStruct.getCode() != null) {
                throw new CustomException(ArticleExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }*/


        return resultDataStruct;
    }

    @Override
    public void modify(Companyinfo companyinfo)throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try{
            Long companyId = companyinfo.getCompanyId();
            if(companyId==null){
                throw new CustomException(CompanyInfoExceptionEnum.COMPANY_INFO_SAVE_FAILED);
            }
            throwException(companyinfo);

            AuthUser authUser = (AuthUser) tokenUtils.getTokenUserInfo();
            companyinfo.setModifier(authUser.getUserId());
            companyinfo.setUpdateTime(System.currentTimeMillis());
            resultMessageStruct =  companyInfoServiceClient.modify(companyinfo);
            if(resultMessageStruct.getCode() != null){
                throw new CustomException(CompanyInfoExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    protected void throwException(Companyinfo companyinfo) throws CustomException {
        if(StringUtils.isEmpty(companyinfo.getCompanyName())){
            throw new CustomException(CompanyInfoExceptionEnum.COMPANY_INFO_NAME_NOT_BLANK);
        }
        if(StringUtils.isEmpty(companyinfo.getCompanyLogo())){
            throw new CustomException(CompanyInfoExceptionEnum.COMPANY_INFO_LOGO_NOT_BLANK);
        }
        if(StringUtils.isEmpty(companyinfo.getBiref())){
            throw new CustomException(CompanyInfoExceptionEnum.COMPANY_INFO_BIEF_NOT_BLANK);
        }
//        if(StringUtils.isEmpty(companyinfo.getCompanyDetails())){
//            throw new CustomException(CompanyInfoExceptionEnum.COMPANY_INFO_DETAILS_NOT_BLANK);
//        }
        if(companyinfo.getOnlineTime()==null){
            throw new CustomException(CompanyInfoExceptionEnum.COMPANY_INFO_ONLINE_NOT_BLANK);
        }
        if(StringUtils.isEmpty(companyinfo.getRegisteredAddress())){
            throw new CustomException(CompanyInfoExceptionEnum.COMPANY_INFO_ADDRESS_NOT_BLANK);
        }
        if(companyinfo.getRegisteredCapital() == null){
            throw new CustomException(CompanyInfoExceptionEnum.COMPANY_INFO_CAPTIAL_NOT_BLANK);
        }
        if(StringUtils.isEmpty(companyinfo.getIncomeRange())){
            throw new CustomException(CompanyInfoExceptionEnum.COMPANY_INFO_INCOME_RAGE_NOT_BLANK);
        }
        if(StringUtils.isEmpty(companyinfo.getCompanyPhone())){
            throw new CustomException(CompanyInfoExceptionEnum.COMPANY_INFO_PHONE_NOT_BLANK);
        }
        /*if(StringUtils.isEmpty(companyinfo.getCompanyCode())){
            throw new CustomException(CompanyInfoExceptionEnum.COMPANY_INFO_CODE_NOT_BLANK);
        }*/
        if(StringUtils.isEmpty(companyinfo.getCompanyPerson())){
            throw new CustomException(CompanyInfoExceptionEnum.COMPANY_INFO_PERSON_NOT_BLANK);
        }
        if(StringUtils.isEmpty(companyinfo.getCompanyContactsPerson())){
            throw new CustomException(CompanyInfoExceptionEnum.COMPANY_INFO_CONTACTS_PERSON_NOT_BLANK);
        }
        if(StringUtils.isEmpty(companyinfo.getIcpRecord())){
            throw new CustomException(CompanyInfoExceptionEnum.COMPANY_INFO_ICP_RECORD_NOT_BLANK);
        }
       /* if(StringUtils.isEmpty(companyinfo.getCompanyProfile())){
            throw new CustomException(CompanyInfoExceptionEnum.COMPANY_INFO_PROFILE_FAILED);
        }*/
        if(companyinfo.getStatus() == null){
            throw new CustomException(CompanyInfoExceptionEnum.COMPANY_INFO_STATUS_FAILED);
        }

    }

    @Override
    public ResultMessageStruct remove(Companyinfo companyinfo) throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try{
            resultMessageStruct =  companyInfoServiceClient.remove(companyinfo);
            if(resultMessageStruct.getCode() != 200){
                throw new CustomException(CompanyInfoExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        resultMessageStruct.setCode(CompanyInfoSuccessEnum.COMPANY_INFO_SUCCESS_ENUM.getCode());
        resultMessageStruct.setMessage(CompanyInfoSuccessEnum.COMPANY_INFO_SUCCESS_ENUM.getMsg());
        return resultMessageStruct;
    }

    @Override
    public ResultMessageStruct sort(Companyinfo companyinfo) {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try{
            resultMessageStruct =  companyInfoServiceClient.sort(companyinfo);
            if(resultMessageStruct.getCode() != 200){
                throw new CustomException(CompanyInfoExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return resultMessageStruct;
    }
}
